// 进程监控事件处理器
class ProcessEventHandler {
    constructor(monitor) {
        this.monitor = monitor;
    }

    // 设置所有事件监听器
    setupEventListeners() {
        // 安全的事件监听器绑定函数
        const safeAddEventListener = (id, event, handler) => {
            const element = document.getElementById(id);
            if (element) {
                element.addEventListener(event, handler);
            } else {
                console.warn(`元素 ${id} 不存在，跳过事件监听器绑定`);
            }
        };

        // 过滤器
        safeAddEventListener('show-listening', 'change', () => {
            this.monitor.filterAndRenderData();
        });

        safeAddEventListener('show-established', 'change', () => {
            this.monitor.filterAndRenderData();
        });

        safeAddEventListener('process-filter', 'input', () => {
            this.monitor.filterAndRenderData();
        });

        safeAddEventListener('port-filter', 'input', () => {
            this.monitor.filterAndRenderData();
        });

        // 模态框关闭
        const closeBtn = document.querySelector('.close');
        if (closeBtn) {
            closeBtn.addEventListener('click', () => {
                this.monitor.closeModal();
            });
        }

        // 全局点击事件（关闭模态框）
        window.addEventListener('click', (e) => {
            const modal = document.getElementById('process-detail-modal');
            if (modal && e.target === modal) {
                this.monitor.closeModal();
            }
        });
    }

    // 设置标签页切换事件
    setupTabs() {
        const tabButtons = document.querySelectorAll('.tab-button');
        const tabContents = document.querySelectorAll('.tab-content');

        tabButtons.forEach(button => {
            button.addEventListener('click', () => {
                const tabName = button.getAttribute('data-tab');
                
                // 移除所有活动状态
                tabButtons.forEach(btn => btn.classList.remove('active'));
                tabContents.forEach(content => content.classList.remove('active'));
                
                // 设置当前活动状态
                button.classList.add('active');
                document.getElementById(`${tabName}-tab`).classList.add('active');
                
                // 重新设置表头排序事件监听器
                if (window.processSorter) {
                    setTimeout(() => {
                        window.processSorter.setupHeaderEventListeners();
                    }, 50);
                }
                
                // 加载对应数据
                if (tabName === 'connections') {
                    this.monitor.loadConnections();
                } else if (tabName === 'processes') {
                    this.monitor.loadProcesses();
                }
            });
        });
    }

    // 处理键盘快捷键
    setupKeyboardShortcuts() {
        document.addEventListener('keydown', (e) => {
            // Escape 关闭模态框
            if (e.key === 'Escape') {
                this.monitor.closeModal();
            }
        });
    }
}

// 导出到全局
window.ProcessEventHandler = ProcessEventHandler;